package com.jsh.erp.redis;

import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.constants.ConfigConstants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * Description
 *
 * @author <a href="https://qiankunpingtai.cn">qiankunpingtai</a>
 * @Date: 2019/6/4 18:10
 */
@Component
public class RedisSession {
    @Resource
    public RedisTemplate redisTemplate;
    /**
     * @author <a href="https://qiankunpingtai.cn">qiankunpingtai</a>
     * description:
     *  从session中获取用户相关信息
     *@date: 2019/6/4 11:36
     * @Param: request
     * @Param: key
     * @return Object
     */
    public  Object getObjectFromSessionByKey(HttpServletRequest request, String key){
        if(request==null||request.getSession()==null){
            return null;
        }
        Object obj=null;
        if(ConfigConstants.redisEnable==1){
            String sessionId=request.getSession().getId().toString();
            //开启redis，用户数据放在redis中，从redis中获取
            if(redisTemplate.opsForHash().hasKey(sessionId,key)){
                //redis中存在，拿出来使用
                obj=redisTemplate.opsForHash().get(sessionId,key);
                redisTemplate.expire(sessionId, BusinessConstants.MAX_SESSION_IN_SECONDS, TimeUnit.SECONDS);
                //session中存放一份，不同的调用时，session可以相互传递
                request.getSession().setAttribute(key,obj);
            }else{
                //redis中没有，说明此项过期，session中去掉
                request.getSession().removeAttribute(key);
            }
        }else{
            //未开启redis，用户数据放在session中，直接从session中获取
            obj = request.getSession().getAttribute(key);
        }
        return obj;
    }
    /**
     * @author <a href="https://qiankunpingtai.cn">qiankunpingtai</a>
     * description:
     *  将用户相关的信息放入session或者redis中
     *@date: 2019/6/4 16:30
     * @Param: request
     * @Param: key
     * @Param: obj
     * @return
     */
    public  void storageObjectBySession( HttpServletRequest request,String key,Object obj) {

        if(request==null||request.getSession()==null){
            return ;
        }
        if(ConfigConstants.redisEnable==1){
            String sessionId=request.getSession().getId().toString();
            //开启redis，用户数据放到redis中
            redisTemplate.opsForHash().put(sessionId,key,obj);
            redisTemplate.expire(sessionId, BusinessConstants.MAX_SESSION_IN_SECONDS, TimeUnit.SECONDS);
        }
            //用户数据放在session中，直接从session中获取
            request.getSession().setAttribute(key,obj);
    }
    /**
     * @author <a href="https://qiankunpingtai.cn">qiankunpingtai</a>
     * description:
     *  将用户相关的信息从session或者redis中移除
     *@date: 2019/6/4 16:30
     * @Param: request
     * @Param: key
     * @Param: obj
     * @return
     */
    public  void deleteObjectBySession(HttpServletRequest request,String key){
        if(request==null||request.getSession()==null){
            return ;
        }
        if(ConfigConstants.redisEnable==1){
            String sessionId=request.getSession().getId().toString();
            //开启redis，用户数据放在redis中，从redis中删除
            redisTemplate.opsForHash().delete(sessionId,key);
        }
            //未开启redis，用户数据放在session中，直接从session中获取
            request.getSession().removeAttribute(key);
    }
}
